package com.lly.songhigh.activity;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

import com.lly.songhigh.R;
import com.lly.songhigh.application.BaseApplication;

import java.util.Date;



/**
 * Created by dllo on 15/10/12.
 * activity的基类
 */
public class BaseActivity extends AppCompatActivity {
    //模式设置
    private BaseApplication baseApplication = null;
    private WindowManager mWindowManager = null;
    private View mNightView = null;
    private boolean mIsAddedView;

    private Long mLastChangeTime;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        baseApplication = (BaseApplication) getApplication();
        if (baseApplication.isNightMode())
            setTheme(R.style.AppTheme_night);
        else
            setTheme(R.style.AppTheme_day);

        super.onCreate(savedInstanceState);
        mIsAddedView = false;
        mLastChangeTime = new Date().getTime();

        if (baseApplication.isNightMode()) {
            initNightView();
//            mNightView.setBackgroundResource(R.color.night_mask);
        }
    }
    public BaseApplication getApp() {
        return baseApplication;
    }

    protected void ChangeToDay() {
        Log.d("TAGGGG", "ChangeToDay() called with: " + "");
        baseApplication.setIsNightMode(false);
        mNightView.setBackgroundResource(android.R.color.transparent);
    }

    protected void ChangeToNight() {
        Log.d("TAGGGG", "ChangeToNight() called with: " + "");
        baseApplication.setIsNightMode(true);
        initNightView();
    }

    //模式设置
    protected void recreateOnResume() {
        new Handler().postDelayed(new Runnable() {
            public void run() {
                recreate();
            }
        }, 100);
    }
    public void initNightView(){
        if (mIsAddedView)
            return;
        WindowManager.LayoutParams mNightViewParam = new  WindowManager.LayoutParams(
                WindowManager.LayoutParams.TYPE_APPLICATION,
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |  WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSPARENT);

        mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        mNightView = new View(this);
        mWindowManager.addView(mNightView, mNightViewParam);
        mIsAddedView = true;
    }
    @Override
    protected void onDestroy() {
        if (mIsAddedView) {
            baseApplication = null;
            mWindowManager.removeViewImmediate(mNightView);
            mWindowManager = null;
            mNightView = null;
        }
        super.onDestroy();
    }
}
